/**
 * <strong>Title : OperationServiceImpl.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.cm.service.impl<br>
 * </strong> <strong>Description : </strong>类注释说明写在此处@<br>
 * <strong>Create on : 2015年9月30日 下午1:48:17<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.cm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.cm.dao.OperationDao;
import cn.b.sky.mes.cm.model.Operation;
import cn.b.sky.mes.cm.model.condition.OperationCondition;
import cn.b.sky.mes.cm.service.OperationService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * <strong>Title : OperationServiceImpl.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.cm.service.impl<br>
 * </strong> <strong>Description : </strong>类注释说明写在此处@<br>
 * <strong>Create on : 2015年9月30日 下午1:48:17<br>
 * </strong>
 * <p>
 * 
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */

@Service(value = "operationService")
public class OperationServiceImpl implements OperationService {

  private static final Logger LOGGER = LoggerFactory.getLogger(OperationServiceImpl.class);

  @Resource
  private OperationDao operationDao;

  /**
   * (非 Javadoc) Title: selectOperationList Description:
   * 
   * @param operationCondition
   * @return
   */
  /**
   * <strong>Title : selectOperationList<br>
   * </strong> <strong>Description : </strong>方法注释说明写在此处@<br>
   * <strong>Create on : 2015年9月30日 下午1:48:17<br>
   * </strong>
   * <p>
   * 
   * @param operationCondition
   * @return
   * @see cn.b.sky.mes.cm.service.OperationService#selectOperationList(cn.b.sky.mes.cm.model.condition.OperationCondition)
   * @throws
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @Override
  public List<Operation> selectOperationList(OperationCondition operationCondition) {

    return operationDao.selectOperationList(operationCondition);

  }

  @Override
  public int getOperationCount(OperationCondition operationCondition) {
    return operationDao.getOperationCount(operationCondition);
  }

  @Override
  public Operation getOperation(String id, String siteCode) {
    OperationCondition operationCondition = new OperationCondition();
    operationCondition.setSiteCode(siteCode);
    operationCondition.setOperationCode(id);
    return operationDao.getOperation(operationCondition);
  }

  @Override
  public int getOperationExactCount(String operationCode, String siteCode) {
    Operation operation = new Operation();
    operation.setSiteCode(siteCode);
    operation.setOperationCode(operationCode);
    return operationDao.getOperationExactCount(operation);
  }

  @Override
  public void insertOperation(Operation operation) {
    operationDao.insertOperation(operation);

  }

  @Override
  public void updateOperation(Operation operation) {
    operationDao.updateOperation(operation);

  }

  @Override
  public BatchJobReport deleteOperations(String operationCode, String siteCode) {
    if (!StringUtils.isBlank(operationCode)) {
      String message = operationCode;
      String cause = "";
      boolean success = true;
      try {

        operationDao.deleteOperation(operationCode, siteCode);
      } catch (Exception e) {
        LOGGER.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(errorMsg)) {
          // 外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(),
              "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    } else {
      return new BatchJobReport("", "id is null", false, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    }

  }

  @Override
  public int getProcessOperationCount(String operationCode, String siteCode) {
    Operation operation = new Operation();
    operation.setSiteCode(siteCode);
    operation.setOperationCode(operationCode);
    return operationDao.getProcessOperationCount(operation);
  }

  @Override
  public List<Operation> selectOperationSuggest(OperationCondition operationCondition) {
    return operationDao.selectOperationSuggest(operationCondition);
  }

}
